﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using caedisi.Common;
using System.ServiceModel.DomainServices.Client;

namespace caedisi.Models
{
    /// <summary>
    /// cellular automata error
    /// </summary>
    public class CAError
    {

        /// <summary>
        /// Gets or sets the error.
        /// </summary>
        /// <value>
        /// The error.
        /// </value>
        public EnumCAError ErrorType { get; set; }

        /// <summary>
        /// Gets the contextual params.
        /// </summary>
        public List<string> ContextualParams { get; private set; }

        /// <summary>
        /// Gets or sets the exception.
        /// </summary>
        /// <value>
        /// The exception.
        /// </value>
        public Exception Exception { get; set; }

        /// <summary>
        /// Gets the exception message.
        /// </summary>
        public string ExceptionMessage
        {
            get
            {
                if (Exception != null)
                    return this.Exception.Message;
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the exception stack trace.
        /// </summary>
        public string ExceptionStackTrace
        {
            get
            {
                if (Exception != null)
                    return this.Exception.StackTrace;
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the inner exception.
        /// </summary>
        public Exception InnerException
        {
            get
            {
                if (Exception != null)
                    return this.Exception.InnerException;
                return null;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LuisError"/> class.
        /// </summary>
        /// <param name="error">The error.</param>
        /// <param name="e">The e.</param>
        /// <param name="contextualParams">The contextual params.</param>
        public CAError(EnumCAError error, Exception e, string[] contextualParams)
        {
            ErrorType = error;
            if (contextualParams == null)
                ContextualParams = new List<string>();
            else
                ContextualParams = new List<string>(contextualParams);
            Exception = e;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LuisError"/> class.
        /// </summary>
        /// <param name="error">The error.</param>
        /// <param name="e">The e.</param>
        public CAError(EnumCAError error, Exception e) : this(error, e, null) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="LuisError"/> class.
        /// </summary>
        /// <param name="e">The e.</param>
        public CAError(Exception e)
        {
            if (e is DomainOperationException)
            {
                DomainOperationException ex = (DomainOperationException)e;
                if (ex.Status == OperationErrorStatus.ServerError)
                {
                    //Unfortunately, we cannot tell if it is IIS issue or SQL. both will give the same status OperationErrorStatus.ServerError
                    ErrorType = EnumCAError.ErrGeneralServerError;
                }
                else
                {
                    ErrorType = EnumCAError.ErrUnknownServerError;
                }
            }
            else
            {
                ErrorType = EnumCAError.ErrUnknownServerError;
            }

            ContextualParams = new List<string>();
            Exception = e;
        }
    }
}
